package View;

/**
 * Creates an inside view of the elevator panel with the available destinations
 * open door and close door options
 * When a destination is chosen the request is sent to the Dispatcher
 * 
 * @author Varun Maryada, Sanat Dixit, Steve Tannahill
 */


import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import Dispatcher.GroupDispatcher;


public class ElevatorGUI extends JPanel implements ActionListener{
	
	private int eleNumber;
	private static final int floorCount=15;
	private JLabel elevatorName;
	private JButton floor1;
	private JButton floor2;
	private JButton floor3;
	private JButton floor4;
	private JButton floor5;
	private JButton floor6;
	private JButton floor7;
	private JButton floor8;
	private JButton floor9;
	private JButton floor10;
	private JButton floor11;
	private JButton floor12;
	private JButton floor13;
	private JButton floor14;
	private JButton floor15;
	
	private JButton open;
	private JButton close;
	
	private GroupDispatcher gd;
	
	/**
	 * Constructs Elevator Panel
	 * @param number
	 * @param gd
	 */
	public ElevatorGUI(int number, GroupDispatcher gd){
		this.elevatorName = new JLabel("Elevator:"+number, JLabel.CENTER);
		this.eleNumber=number;
		this.gd = gd;
		this.initButtons();
		this.setLayout(new GridLayout(8, 1));
	}
	
	/**
	 * Creates Floor Buttons
	 */
	public void initButtons(){
		add(new JLabel());
		add(elevatorName);
		add(new JLabel());
		floor1 = new JButton("1");floor1.addActionListener(this);
		add(floor1);
		floor2 = new JButton("2");floor2.addActionListener(this);
		add(floor2);
		floor3 = new JButton("3");floor3.addActionListener(this);
		add(floor3);
		floor4 = new JButton("4");floor4.addActionListener(this);
		add(floor4);
		floor5 = new JButton("5");floor5.addActionListener(this);
		add(floor5);
		floor6 = new JButton("6");floor6.addActionListener(this);
		add(floor6);
		floor7 = new JButton("7");floor7.addActionListener(this);
		add(floor7);
		floor8 = new JButton("8");floor8.addActionListener(this);
		add(floor8);
		floor9 = new JButton("9");floor9.addActionListener(this);
		add(floor9);
		floor10 = new JButton("10");floor10.addActionListener(this);
		add(floor10);
		floor11 = new JButton("11");floor11.addActionListener(this);
		add(floor11);
		floor12 = new JButton("12");floor12.addActionListener(this);
		add(floor12);
		floor13 = new JButton("13");floor13.addActionListener(this);
		add(floor13);
		floor14 = new JButton("14");floor14.addActionListener(this);
		add(floor14);
		floor15 = new JButton("15");floor15.addActionListener(this);
		add(floor15);
		
		
		open = new JButton("Open Door");open.addActionListener(this);
		add(open);
		add(new JLabel());
		close = new JButton("Close Door");close.addActionListener(this);
		add(close);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == close){
			this.gd.receivedElevatorCall(-1);
		}
		else if(e.getSource() == open){
			this.gd.receivedElevatorCall(0);
		}
		else{
			this.gd.receivedElevatorCall(Integer.parseInt(((JButton)e.getSource()).getText()));
		}
		
	}
	
}
